package gui.productgroup;

import core.*;
import gui.common.*;
import gui.inventory.*;

/**
 * Controller class for the edit product group view.
 */
public class EditProductGroupController extends Controller 
										implements IEditProductGroupController {
	
	private CoreFacade core;
	private boolean canEdit;
	private String currentName;
	private String nameToEdit, valueToEdit;
	private SizeUnits unitToEdit;
	private ProductContainer parent;
	private Amount oldAmount;
	private ProductGroup old;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit product group view
	 * @param target Product group being edited
	 */
	public EditProductGroupController(IView view, ProductContainerData target) {
		super(view);
		
		canEdit = true;

		core = CoreFacade.getInstance();
		
		old =(ProductGroup)target.getTag();
		
		parent = old.getParentContainer();
		
		currentName = target.getName();
		
		nameToEdit = currentName;
		
		valueToEdit = Float.toString(old.getThreeMonthSupplyAmount().getSize());
		
		unitToEdit = old.getThreeMonthSupplyAmount().getUnit().toSizeUnits();
		
		oldAmount = old.getThreeMonthSupplyAmount();
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductGroupView getView() {
		return (IEditProductGroupView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		getView().enableOK(canEdit);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		getView().setProductGroupName(currentName);
		getView().setSupplyUnit(oldAmount.getUnit().toSizeUnits());
		float f = oldAmount.getSize();
		if(oldAmount.getUnit() == Unit.count)
			getView().setSupplyValue(Integer.toString((int)f));
		else
			getView().setSupplyValue(Float.toString(f));
	}

	//
	// IEditProductGroupController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product group view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		nameToEdit = getView().getProductGroupName();
		valueToEdit = getView().getSupplyValue();
		unitToEdit = getView().getSupplyUnit();
		float value;
		try{
			value = Float.parseFloat(valueToEdit);
		} catch (Exception e){
			value = -1;
		}
		Amount amt = new Amount(unitToEdit.toUnit(), value);
		boolean validAmt = amt.isValidAmount();
		
		boolean sameName = nameToEdit.equals(currentName);
		
		boolean canAdd = core.canAddProductGroup(parent, nameToEdit, value, unitToEdit.toUnit());
		
		canEdit = canAdd || (validAmt && sameName);
		/*if(nameToEdit.equals(currentName))
			canEdit = true;*/
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product group view.
	 */
	@Override
	public void editProductGroup() 
	{
		ProductGroup pg = new ProductGroup(nameToEdit, new Amount(unitToEdit.toUnit(), 
											Float.parseFloat(valueToEdit)),parent);
	
		try{
			core.editProductGroup(old, pg);
		}
		catch(Exception e)
		{
			
		}
	}

}

